
var _msc_json=null;
function _MobileSelectCategory_Select(index){
    if(!_msc_json){return;}
    var item = _msc_json.items[index];
    if(_msc_json.event){
        _msc_json.event(item);
    }
    if(_msc_json.func){
        _msc_json.func(item);
    }
    $("#_MobileDialog_Category").css("display","none");
}
function MobileAppendHtmlToBody(id,xml){
    var obj = document.getElementById(id);
    if(obj){
        $("#"+id).html(xml);
        $("#"+id).css("display","flex");
    }else{
        $("body").append(xml);
    }
}
function MobileSelectCategory(j){
    _msc_json = j;
    var xml=`
    <div class="MobilePopupMask" id='_MobileDialog_Category' onclick="_HideSelfDiv(event)">
		<div class="MobileCategoryList"  onclick="event.stopPropagation();">
        `;
    if(j.title){
        xml+=`<div class="MobileCategoryItem MobileCategoryTitle">${j.title}</div>`;
    }
    var i=0;
    if(j.items){
    for(let t of j.items){
        xml+=`<div onclick="_MobileSelectCategory_Select(${i});" class="MobileCategoryItem">${t}</div>`;
        i++;
    }}
    xml+=`
		</div>
	</div>`;

    MobileAppendHtmlToBody('_MobileDialog_Category', xml);
}

var _mobile_bj;
function _HideSelfDiv(e){
    $("#"+e.target.id).css("display","none");
    //console.log(e.target.id);
}
function _MobileMessageDialog_Button(index){
    if(!_mobile_bj){return;}
    if(!_mobile_bj.buttons){return;}

    var b = _mobile_bj.buttons[index];
    if(!b){return;}

    var f = b.callback||b.func||b.event||b.f;
    if(f){
        f();
    }else{

    }
    $("#_MobileDialog_Message").css("display","none");
}
function MobileMessageDialog(j){
    _mobile_bj = j;
    var xml=`
    <div class="MobilePopupMask" id="_MobileDialog_Message" onclick="_HideSelfDiv(event)">
		<div class="MobileMsgDialog" onclick="event.stopPropagation();">
			<div class="MobileMsgDialogContent">`;
            if(j.title){
                xml+=`<h2>${j.title}</h2>`;
            }
            if(j.text||subtitle){
                xml+=`<p>${j.text||j.subtitle}</p>`;
            }
            xml+='</div>';
            if(j.buttons){
                xml+=`<div class="MobileDialogButtons">`;
                var bi=0;
                for(let b of j.buttons){
                    if(typeof b == 'string'){
                        xml+=`<button  onclick="_MobileMessageDialog_Button(${bi})"  class='MobileDialogButton_white'>${b}</button>`;
                    }else{
                        var name = b.name||b.text;
                        var c = 'white';
                        if(b.color){c=b.color;}
                        xml+=`<button onclick="_MobileMessageDialog_Button(${bi})" class='MobileDialogButton_${c}'>${name}</button>`;
                    }
                    bi++;
                }
                xml+=`</div>`;
            }
          
    xml+=`</div>
	</div>`;

    MobileAppendHtmlToBody('_MobileDialog_Message', xml);
}